package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.ActionBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * 场景界面选择设备并排序的适配器
 * Created by David on 2015/8/17.
 */
public class SceneActionDragSortListViewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<ActionBean> items;//适配器的数据源

    /**
     * 构造函数
     *
     * @param context
     * @param list    可控制的执行类列表
     */
    public SceneActionDragSortListViewAdapter(Context context, ArrayList<ActionBean> list) {
        this.context = context;
        this.items = list;
        if (items == null) {
            items = new ArrayList<ActionBean>();
        }
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int arg0) {
        return items.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    public void remove(int arg0) {//删除指定位置的item
        items.remove(arg0);
        this.notifyDataSetChanged();//不要忘记更改适配器对象的数据源
    }

    public void insert(ActionBean item, int arg0) {
        //在指定位置插入item
        items.add(arg0, item);
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.draglistview_item_action, null);
            holder.tvOrder = (TextView) convertView.findViewById(R.id.tv_order);
            holder.tvActionName = (TextView) convertView.findViewById(R.id.tv_action_name);
            holder.tvCondition = (TextView) convertView.findViewById(R.id.tv_condition);
            holder.ivDeviceIcon = (ImageView) convertView.findViewById(R.id.iv_device_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ActionBean item = (ActionBean) getItem(position);
        item.setOrder(position + 1);
        holder.tvOrder.setText(String.valueOf(position + 1));
        String address = item.getAddress();
        DeviceBean deviceBean;
        if (address != null) {
            if (item.getValue() == 255) {
                holder.tvCondition.setText(context.getString(R.string.condition_open));
            } else if (item.getValue() == 0) {
                holder.tvCondition.setText(context.getString(R.string.condition_close));
            }
            deviceBean = SystemConstant.deviceBeanHashMap.get(item.getAddress());
            if (deviceBean != null) {
                String tag = deviceBean.getUserTag();
                if (tag != null) {
                    if (DeviceConstant.nameMap.get(tag) == null) {
                        holder.tvActionName.setText(tag);
                    } else {
                        holder.tvActionName.setText(DeviceConstant.nameMap.get(tag));
                    }
                } else {
                    holder.tvActionName.setText(deviceBean.getName());
                }
            }
            if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
                holder.ivDeviceIcon.setImageResource(R.drawable.ph_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                holder.ivDeviceIcon.setImageResource(R.drawable.temperature_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
                if (deviceBean.getUserTag() != null) {
                    if (deviceBean.getUserTag().equals("Light Switch")) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                    } else if (deviceBean.getUserTag().equals("Feed Switch")) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
                    } else if (deviceBean.getUserTag().equals("Water Switch")) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.water_switch_icon);
                    } else if (deviceBean.getUserTag().equals("O2 Switch")) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                    } else {
                        holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                    }
                } else {
                    holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                }
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
                holder.ivDeviceIcon.setImageResource(R.drawable.water_lever_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
                holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
            } else {
                holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
            }
        } else {
            holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
            holder.tvActionName.setText(context.getString(R.string.device_action_wait));
            double time = item.getValue();
            holder.tvCondition.setText(String.valueOf(time) + context.getString(R.string.sleep_unit));
        }
        return convertView;
    }

    //更新数据
    public void update(ArrayList<ActionBean> commands) {
        items = commands;
        notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tvOrder;
        TextView tvActionName;
        TextView tvCondition;
        ImageView ivDeviceIcon;
    }
}
